home comics writing pictures archive about

RecurringExpense.cs

Language: C#
Last Modified: 2020-06-27 1:58:31 PM UTC
File Size: 4881 bytes
http://www.penguinstew.ca/example/dataviewer/Model/RecurringExpense.cs
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Collections.Generic;
namespace Budgeter.Model
{
[Table(Name = "RecurringPayments")]
public class RecurringPayment : IRefreshableTable
{
#region Constructor
public RecurringPayment()
{
RecurringPaymentMonths = new EntitySet<RecurringPaymentMonth>();
RecurringPaymentItems = new EntitySet<RecurringPaymentItem>();
}
#endregion
#region Columns
/// <summary>
/// Unique ID
/// </summary>
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int RecurringPaymentId { get; set; }
/// <summary>
/// The name of the recurring Payment
/// </summary>
[Column(DbType = "nvarchar(150) NOT NULL")]
public string Name { get; set; }
/// <summary>
/// Controls the order to display the recursing months
/// </summary>
[Column]
public int Order { get; set; }
/// <summary>
/// The item type
/// </summary>
[Column(DbType = "INT NOT NULL")]
public Enums.PaymentDirection Direction { get; set; }
/// <summary>
/// The item type
/// </summary>
[Column(DbType = "INT NOT NULL")]
public Enums.PaymentCalculationType Type { get; set; }
/// <summary>
/// True if still being used
/// </summary>
[Column]
public bool InUse { get; set; }
#endregion
#region Entities
private EntitySet<RecurringPaymentMonth> m_recurringPaymentMonths;
/// <summary>
/// Reference to the related recurring Payment months
/// </summary>
[Association(Storage = "m_recurringPaymentMonths", OtherKey = "RecurringPaymentId")]
public EntitySet<RecurringPaymentMonth> RecurringPaymentMonths
{
get
{
return m_recurringPaymentMonths;
}
set
{
m_recurringPaymentMonths = value;
}
}
private EntitySet<RecurringPaymentItem> m_recurringPaymentItems;
/// <summary>
/// Reference to the related recurring Payment items
/// </summary>
[Association(Storage = "m_recurringPaymentItems", OtherKey = "RecurringPaymentId")]
public EntitySet<RecurringPaymentItem> RecurringPaymentItems
{
get
{
return m_recurringPaymentItems;
}
set
{
m_recurringPaymentItems = value;
}
}
#endregion
#region IRefreshableTable
public void RefreshEntitySets(Connection.BudgeterDataContext connection)
{
RecurringPaymentMonths.Clear();
RecurringPaymentMonths.AddRange(connection.RecurringPaymentMonths.Where(m => m.RecurringPaymentId == RecurringPaymentId));
RecurringPaymentItems.Clear();
RecurringPaymentItems.AddRange(connection.RecurringPaymentItems.Where(i => i.RecurringPaymentId == RecurringPaymentId));
}
#endregion
#region Public methods
public decimal CalculateAmount()
{
if (Type == Enums.PaymentCalculationType.Item)
{
decimal amount = 0;
foreach (var item in m_recurringPaymentItems.Where(i => i.Parent == null))
{
amount += item.Amount;
}
return amount;
}
if (m_recurringPaymentMonths.Count == 0)
{
return 0;
}
List<decimal> actualValuePerMonth = new List<decimal>();
foreach (var month in m_recurringPaymentMonths)
{
decimal actualAmount = month.ActualAmount();
if (actualAmount != 0)
{
actualValuePerMonth.Add(actualAmount);
}
}
switch (Type)
{
case Enums.PaymentCalculationType.Average:
return actualValuePerMonth.Average();
case Enums.PaymentCalculationType.Max:
return actualValuePerMonth.Max();
case Enums.PaymentCalculationType.Min:
return actualValuePerMonth.Min();
default:
return 0;
}
}
#endregion
#region Override
public override string ToString()
{
return Name;
}
#endregion
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169